package ee.rawre.tafayc.client;

import ee.rawre.tafayc.util.ResultChart;
import ee.rawre.tafayc.util.TypingStatus;
import ee.rawre.tafayc.util.CookieManager;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;

public class Tafayc implements EntryPoint {
	   
      private int curLenght, lastLenght, top, left, inputHeight=200, inputWidth=500;
      private boolean finish;
      private Timer timer;
	   private TypingStatus typingStatus;
	   private String textIn;
	   private DecoratedPopupPanel popup;
	   private TextArea textInput = new TextArea();
	   private TextArea textArea = new TextArea();
	   private Anchor textinputLink, viewResultsLink;
	   private CookieManager cookieManager;
	   private ResultChart resultChart;
	   private CheckBox characters;
	   
      public void onModuleLoad() {
         init();
		}
      
      private void init() {
         cookieManager = new CookieManager();
         
         System.out.println("--------LOADED--------");
         
         // MENUBAR
         HorizontalPanel menuBar = new HorizontalPanel();
         menuBar.setStyleName("menuBar");
         menuBar.add(textinputLink = new Anchor("Insert new text"));
         menuBar.add(viewResultsLink = new Anchor("View previous results"));
         menuBar.add(characters = new CheckBox());
         characters.addValueChangeHandler(new CheckBoxValueChangeHandler());
         
         textinputLink.addClickHandler(new TextinputLinkClickHandler());
         viewResultsLink.addClickHandler(new ViewResultsLinkClickHandler());
         
         // TEXTAREA
         textArea.setPixelSize(inputWidth, inputHeight);
         textArea.addFocusHandler(new TextAreaFocusHandler());
         
         // DISPLAYING
         RootPanel.get("menuBar").add(menuBar);
         RootPanel.get("typeBoxContainer").add(textArea);

         textArea.setFocus(true);
      }
      
      private void getText() {
         typingStatus = new TypingStatus(cookieManager);
         if (!textInput.getText().isEmpty()) {
            typingStatus.setText(textInput.getText());
         }
         RootPanel.get("typeStatusContainer").clear();
         RootPanel.get("typeStatusContainer").add(typingStatus);
      }
		
		private class TextAreaFocusHandler implements FocusHandler {
		  
		   @Override
         public void onFocus(FocusEvent event) {
            getText();
            timer = new Timer() {
               @Override
               public void run() {
                  textIn = textArea.getText();
                  curLenght = textIn.length();
                  if (curLenght == lastLenght+1) {
                     finish = typingStatus.keyPress(textIn.charAt(curLenght-1));
                  } else if (curLenght < lastLenght) {
                     typingStatus.backspacePress(curLenght);
                  } else if (curLenght > lastLenght/*+1*/) {
                     typingStatus.keyPresses(textIn.substring(lastLenght, curLenght));
                  }
//                  if (finish) {
//                     textArea.setFocus(false); // a bad idea
//                  }
                  lastLenght = curLenght;
               }
            };
            timer.scheduleRepeating(50);
         }
		}
		
		private class TextinputLinkClickHandler implements ClickHandler {

         @Override
         public void onClick(ClickEvent event) {
            popup = new DecoratedPopupPanel(true);
            popup.setGlassEnabled(true);
            textInput.setPixelSize(inputWidth, inputHeight);
            textInput.addKeyPressHandler(new InsertNewTextKeyPressHandler());
            popup.setWidget(textInput);
            top = textArea.getAbsoluteTop();
            left = textArea.getAbsoluteLeft();
            popup.setPopupPosition(left-10, top-21);
            popup.show();
         }
		   
		}
		
		private class ViewResultsLinkClickHandler implements ClickHandler {

         @Override
         public void onClick(ClickEvent event) {
            popup = new DecoratedPopupPanel(true);
            popup.setGlassEnabled(true);
            
            resultChart = new ResultChart(cookieManager);
            
//            resultChart.setHTML(cookieManager.toString()); // debug
            
            popup.add(resultChart);
            popup.center();
            popup.show();
         }
         
      }
		
		 
		
		private class InsertNewTextKeyPressHandler implements KeyPressHandler {

         @Override
         public void onKeyPress(KeyPressEvent event) {
            if (event.getCharCode() == KeyCodes.KEY_ENTER) {
               getText();
               popup.hide();
               textArea.setFocus(true);
            }
         }


		   
		}
		
		private class CheckBoxValueChangeHandler implements ValueChangeHandler<Boolean> {

         @Override
         public void onValueChange(ValueChangeEvent<Boolean> event) {
            if(event.getValue()) {
               System.out.println("ON");
            } else {
               System.out.println("OFF");
            }
         }
         
      }

	}